package ru.exigenservices.intern.dao.hibernate;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.orm.hibernate3.HibernateTemplate;
import ru.exigenservices.intern.dao.GroupDao;
import ru.exigenservices.intern.model.Contact;
import ru.exigenservices.intern.model.Group;

import java.util.List;

public class GroupDaoHibernate implements GroupDao {

    public List<Group> getAll() {
        return hibernateTemplate.loadAll(Group.class);
    }

    public Group get(Long id) {
        return hibernateTemplate.load(Group.class, id);
    }

    public Group delete(Long id) {
        Group group = get(id);
        hibernateTemplate.delete(group);
        return group;
    }

    public Group save(Group group) {
        hibernateTemplate.saveOrUpdate(group);
        return get(group.getId());
    }

    @Required
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    private HibernateTemplate hibernateTemplate;
    private SessionFactory sessionFactory;
}
